package com.bright.hxj.qchz.service.impl;

import com.bright.hxj.qchz.pojo.bo.DuijieReturnMsg;
import com.bright.hxj.qchz.pojo.command.SubmitTaskCommand;
import com.bright.hxj.qchz.pojo.po.TaskAsync;
import com.bright.hxj.qchz.service.AbstractSubmitTaskService;
import com.bright.hxj.qchz.service.Duijie_ShengJieShou_QCHZManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.RejectedExecutionException;

/**
 * @Author hxj
 * @Date 2025/1/7 11:28
 * @Description 使用线程池提交任务
 */
@Component
@RequiredArgsConstructor
@ConditionalOnProperty(name = "submit-task.type", havingValue = "threadPool", matchIfMissing = true)
@Slf4j
public class ThreadPoolSubmitTaskServiceImpl extends AbstractSubmitTaskService {

    private final ThreadPoolTaskExecutor executorService;

    @Override
    public DuijieReturnMsg submitTask(SubmitTaskCommand command, TaskAsync taskAsync) {
        Duijie_ShengJieShou_QCHZManager qchzManager = command.getQchzManager();
        String taskId = command.getTaskId();
        // 启动线程
        try {
            CompletableFuture.supplyAsync(() ->
                                // 线程中的业务处理
                                qchzManager.processBusinessAsyncInThread(taskAsync)
                        , executorService);
//                .exceptionally((error) -> {
//                    if (error instanceof RejectedExecutionException) {
//                        log.info("线程池满，拒绝服务" + command.getTaskId());
//                        return new DuijieReturnMsg("服务繁忙，请稍候再试！");
//                    }
//
//
//                    // 任务出错 更新数据库中的任务状态
//                    taskManager.updateTaskStatus(command.getTaskId(), taskAsync.getUserName(), TaskManager.ASYNC_TASK_ERROR, "任务执行出错！" + error.getMessage(), null);
//
//                    log.error("线程执行出错！", error);
//                    //            System.out.println("出错！" + error.getMessage());
//                    return new DuijieReturnMsg("任务处理出错！请联系管理员！" + command.getTaskId());
//                });

//        String taskId = command.getTaskId();
////        Duijie_QCHZ_ProcessAsyncThread thread = new Duijie_QCHZ_ProcessAsyncThread(taskId, command.getMethodName(),
////                command.getZtId(), command.getUser(), command.getJsonObject(), command.getQchzManager(), command.getTaskManager());
//
//        // 生成同步任务号 如果线程池满 则不生成 返回异常
//        try {
//            executorService.submit(() -> {
//
//            });
        } catch (RejectedExecutionException e) {
            log.info("线程池满，拒绝服务" + command.getTaskId());
            return new DuijieReturnMsg("服务繁忙，请稍候再试！");
        } catch (Throwable e) {
            log.error(taskId + "处理出错", e);
            return new DuijieReturnMsg("创建任务处理出错，请联系管理员！" + taskId);
        }
        return new DuijieReturnMsg(true, null);
    }

}
